package nxu.it.movieapi.service.mongo;

import nxu.it.api.common.result.Pageable;
import nxu.it.api.model.Movie;
import nxu.it.api.model.SimpleMovie;
import nxu.it.api.service.MovieService;
import nxu.it.api.service.param.MoviePageQueryParam;
import nxu.it.movieapi.Doc.MovieDoc;
import nxu.it.movieapi.service.mapper.MovieMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Component
public class MovieServiceMongoImpl implements MovieService {
    @Autowired
    MongoTemplate template;

    @Autowired
    MovieMapper movieMapper;

    @Override
    public Optional<Movie> findById(@NotNull Integer movieId) {

        MovieDoc movieDoc = template.findById(movieId, MovieDoc.class);
        Movie movie = movieMapper.fromDoc(movieDoc);
        return Optional.ofNullable(movie);
    }

    @Override
    public Pageable<SimpleMovie> page(@NotNull MoviePageQueryParam param) {
        List<Criteria> allCriteria = new ArrayList<>();
        String originalTitle = param.getOriginalTitle();
        if (originalTitle != null && !originalTitle.isBlank()) {
            allCriteria.add(Criteria.where("originalTitle").regex(originalTitle, "i"));
        }

        String chineseTitle = param.getChineseTitle();
        if (chineseTitle != null && !chineseTitle.isBlank()) {
            allCriteria.add(Criteria.where("chineseTitle").regex(chineseTitle, "i"));
        }

        String language = param.getLanguage();
        if (language != null && !language.isBlank()) {
            allCriteria.add(Criteria.where("language").regex(language, "i"));
        }
        String region = param.getRegion();
        if (region != null && !region.isBlank()) {
            allCriteria.add(Criteria.where("region").regex(region, "i"));
        }

        Integer startYear = param.getStartYear();
        if (startYear != null) {
            allCriteria.add(Criteria.where("year").gte(startYear));
        }

        Integer endYear = param.getEndYear();
        if (endYear != null) {
            allCriteria.add(Criteria.where("year").lte(endYear));
        }
        List<Integer> genreIds = param.getGenreIds();
        if (genreIds != null && !genreIds.isEmpty()) {
            allCriteria.add(Criteria.where("genres.id").all(genreIds));
        }
        Criteria c = new Criteria();
        if (!allCriteria.isEmpty()) {
            c=c.andOperator(allCriteria);
        }
        Query query = new Query();
        query.addCriteria(c);
        long totalCount = template.count(query, Movie.class);
        int pageSize=param.getPageSize();
        int pageNumber=param.getPageNumber();

        //int totalPage= PageUtil.getTotalPage((int)totalCount,pageSize);
        //int offset=PageUtil.getOffest(pageNumber,pageSize);


        //query.skip(offset).limit(pageSize);
        List<MovieDoc> movieDocList = template.find(query, MovieDoc.class);
        List<SimpleMovie> movieList=movieMapper.fromDocListToSimpleMovieList(movieDocList);
        //BasePage<SimpleMovie> simpleMovieBasePage=new BasePage<>(movieList,pageNumber,pageSize);

        return MovieService.super.page(param);
    }
}
